# coding=utf-8

# 这应该是单层神经元网络，只有一层隐藏层（hidden layer）。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import pylab

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

tf.reset_default_graph()

x = tf.placeholder(tf.float32, [None, 784]) # input data
y = tf.placeholder(tf.float32, [None, 10])  # outputs

W = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))

pred = tf.nn.softmax(tf.matmul(x, W) + b) # 正向模型

# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))

learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)


batch_size = 100

with tf.Session() as s:
    s.run(tf.global_variables_initializer())

    for epoch in range(25): # train 50 times
        total_batch = int(mnist.train.num_examples/batch_size)
        for i in range(total_batch):
            batch_x, batch_y = mnist.train.next_batch(batch_size)
            # train with data set
            acc, loss = s.run([optimizer, cost], feed_dict={x:batch_x, y:batch_y})
            print(acc, loss)